package android.bignerdranch.xiangmu;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;



public class MessageSave extends AppCompatActivity implements View.OnClickListener {
    private static final String PREFS_NAME ="android.bignerdranch.xiangmu" ;
    EditText et_name;
    EditText et_id;
    EditText et_classes;
    Button bt_ms;
    Button bt_home;
    Button bt_clear;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.messagesave);
        Init();
    }

    void Init()
    {
        et_name = findViewById(R.id.et_name);
        et_id = findViewById(R.id.et_id);
        et_classes = findViewById(R.id.et_classes);
        bt_ms = findViewById(R.id.ms);
        bt_home = findViewById(R.id.re_main);
        bt_clear = findViewById(R.id.clear);
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        if(sharedPreferences.getInt("count",0) == 0)
            editor.putInt("count",1);
        editor.apply();
        bt_ms.setOnClickListener(this);
        bt_home.setOnClickListener(this);
        bt_clear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final int bt_ms = R.id.ms;
        final int bt_home = R.id.re_main;
        final int bt_clear = R.id.clear;
        switch (v.getId())
        {
            case bt_home:
            {
                Intent intent = new Intent();
                intent.setClass(MessageSave.this,Main.class);
                startActivity(intent);
                break;
            }
            case bt_ms:
            {
                MyHelper myHelper = new MyHelper(this);
                SQLiteDatabase db = myHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                String meg_name = "Student"+sharedPreferences.getInt("count",0)+"name";
                String meg_id = "Student"+sharedPreferences.getInt("count",0)+"id";
                String meg_classes = "Student"+sharedPreferences.getInt("count",0)+"classes";
                editor.putString(meg_name,et_name.getText().toString());
                editor.putString(meg_id,et_id.getText().toString());
                editor.putString(meg_classes,et_classes.getText().toString());
                values.put("name", et_name.getText().toString());
                values.put("id", et_id.getText().toString());
                values.put("class", et_classes.getText().toString());
                values.put("state", "已到");
                int count = sharedPreferences.getInt("count",0) + 1;
                editor.apply();
                Toast.makeText(MessageSave.this,"Student"+sharedPreferences.getInt("count",0)+"添加成功",Toast.LENGTH_SHORT).show();
                editor.putInt("count",count);
                long q = db.insert("information",null, values);
                editor.apply();
                break;
            }
            case bt_clear:
            {
                int count = sharedPreferences.getInt("count",-1);
                for(int i = count;i>=1;i--)
                {
                    String meg_name = "Student"+i+"name";
                    String meg_id = "Student"+i+"id";
                    String meg_classes = "Student"+i+"classes";
                    editor.putString(meg_name,null);
                    editor.putString(meg_id,null);
                    editor.putString(meg_classes,null);
                    editor.putInt("count",i);
                    editor.apply();
                    Toast.makeText(MessageSave.this,"Student"+i+"清空完毕",Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(MessageSave.this,"清空完毕",Toast.LENGTH_SHORT).show();
            }
        }
    }
}
